/// @tags:
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

typedef long long ll;

constexpr int maxn = 100, mod = 998244353;

int p[maxn];
ll pow[maxn];
bool vis[maxn];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }
  ll ans = 0, mul = 1;
  pow[0] = 1;
  for (int i = 1; i <= n; ++i) {
    pow[i] = pow[i - 1] * m % mod;
  }
  int cnt = n;
  for (int i = 1; i <= n; ++i) {
    if (vis[i] || vis[p[i]]) continue;
    if (p[i] == i) {
      cnt--;
      mul = mul * m % mod;
    } else {
      vis[i] = vis[p[i]] = true;
      cnt -= 2;
      ans += (ll)m * (m - 1) / 2 % mod * mul % mod * pow[cnt] % mod;
      ans %= mod;
      mul = mul * m % mod;
    }
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("B - A < AP.in", "r", stdin);
  freopen("B - A < AP.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
